<?php
// 获取系统设置从settings.json文件

header('Content-Type: application/json');

// 只处理GET请求
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    http_response_code(405);
    echo json_encode(['status' => 'error', 'message' => '只允许GET请求']);
    exit;
}

try {
    // 检查settings.json文件是否存在
    if (file_exists('settings.json')) {
        // 读取文件内容
        $jsonContent = file_get_contents('settings.json');
        $settings = json_decode($jsonContent, true);
        
        if ($settings) {
            echo json_encode(['status' => 'success', 'settings' => $settings]);
        } else {
            throw new Exception('文件内容无效');
        }
    } else {
        // 如果文件不存在，返回默认设置
        $defaultSettings = [
            'param1' => '30',
            'param2' => 'Celsius',
            'param3' => '80',
            'param4' => '-10',
            'param5' => '30',
            'param6' => '60',
            'param7' => 'admin@example.com',
            'param8' => 'INFO',
            'param9' => 'TemperatureSensor1',
            'param10' => 'Room1'
        ];
        echo json_encode(['status' => 'success', 'settings' => $defaultSettings]);
    }
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => '读取设置失败: ' . $e->getMessage()]);
}
?>